var express = require("express");
var router = express.Router();
const mysql = require("../util/mysql/mysql");

//我的消息
router.post("/mymes", function (req, res, next) {
  mysql.data("select * from my_mes", [], function (err, rows, fields) {
    if (err) {
      console.log(err);
      return;
    }
    res.send(JSON.stringify(rows));
  });
});
//查看我的消息
router.post("/lookMymes", function (req, res, next) {
  mysql.data(
    "select * from my_mes where id=" + req.body.id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows[0]));
    }
  );
});
//删除我的消息
router.post("/delMymes", function (req, res, next) {
  mysql.data(
    "delete from my_mes where id=" + req.body.id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//最近浏览
router.post("/browse", function (req, res, next) {
  mysql.data(
    "SELECT * from goods a INNER JOIN recently_browse b ON b.good_id=a.id",
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows.reverse()));
    }
  );
});

//删除最近浏览
router.post("/delBrowse", function (req, res, next) {
  mysql.data(
    "delete from recently_browse where good_id=" + req.body.good_id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//添加收藏
router.post('/addcollect', function (req, res, next) {
  const id = req.body.id
  console.log(id);
  mysql.data('insert into my_collect(good_id) values(?)', [id], function (err, rows, fields) {
    if (err) {
      console.log(err);
      return;
    }
    res.send(JSON.stringify({ code: 1 }));

  });
})
//我的收藏
router.post("/collect", function (req, res, next) {
  mysql.data(
    "SELECT * from goods a INNER JOIN my_collect b ON b.good_id=a.id",
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows.reverse()));
    }
  );
});
//删除最近浏览
router.post("/delCollect", function (req, res, next) {
  mysql.data(
    "delete from my_collect where good_id=" + req.body.good_id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//我的收货地址
router.post("/mysite", function (req, res, next) {
  mysql.data(
    "SELECT * from my_site where u_id=" + req.body.u_id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows));
    }
  );
});
//设为默认地址
router.post("/defaultSite", function (req, res, next) {
  mysql.data("UPDATE my_site SET muo=0 ", [], function (err, rows, fields) {
    if (err) {
      console.log(err);
      return;
    }
    mysql.data(
      "UPDATE my_site SET muo=1 where site_id=" + req.body.id,
      [],
      function (err, rows, fields) {
        if (err) {
          console.log(err);
          return;
        }
        res.send(JSON.stringify({ code: 1 }));
      }
    );
  });
});
//删除地址信息
router.post("/delSite", function (req, res, next) {
  mysql.data(
    "delete from my_site where site_id=" + req.body.site_id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//个人资料
router.post("/personald", function (req, res, next) {
  mysql.data(
    "select * from userdata where u_id=" + req.body.id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows[0]));
    }
  );
});
//换绑手机号
router.post("/changePhone", function (req, res, next) {
  mysql.data(
    "UPDATE userdata SET u_phone=" +
    req.body.phone +
    " where u_id=" +
    req.body.id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
// 修改个人资料
router.post("/changePersonald", function (req, res, next) {
  mysql.data(
    "UPDATE userdata SET ni_name='" +
    req.body.ni_name +
    "',sex=" +
    req.body.sex +
    ",birthday='" +
    req.body.birthday +
    "' where u_id=" +
    req.body.id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
//修改密码
router.post("/changepossword", function (req, res, next) {
  const password = req.body.password
  mysql.data('update userdata set u_pass=? where u_id=1', [password], function (err, rows, fields) {
    if (err) {
      console.log(err);
      return;
    }
    res.send(JSON.stringify({ code: 1 }));
  }
  );
});
//我的钱包充值
router.post("/myWallet", function (req, res, next) {
  mysql.data(
    "UPDATE userdata SET wallet=wallet+" +
    req.body.wallet +
    " where u_id=" +
    req.body.id,
    [],
    function (err1, rows1, fields) {
      if (err1) {
        console.log(err1);
        return;
      }
      mysql.data(
        "select wallet from userdata where u_id=1",
        [],
        function (err2, rows2, fields) {
          if (err2) {
            console.log(err2);
            return;
          }
          mysql.data(
            "INSERT INTO my_record(r_type,r_num,r_time,r_yu) VALUES (?,?,?,?)",
            ['充值', req.body.wallet, '2021-12-13', rows2[0].wallet],
            function (err3, rows, fields) {
              if (err3) {
                console.log(err3);
                return;
              }
              res.send(JSON.stringify({ code: 1 }));
            }
          );
        }
      );

    }
  );
});
//我的账户
router.post("/myAccount", function (req, res, next) {
  mysql.data(
    "select * from userdata where u_id=" + req.body.id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows[0]));
    }
  );
});
//我的订单
router.post("/myBill", function (req, res, next) {
  mysql.data("SELECT * from my_bill", [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      var b_arr = [];
      for (var i = 0; i < rows.length; i++) {
        b_arr.push(rows[i].b_id);
      }
      var prom = [];
      for (var i = 0; i < b_arr.length; i++) {
        prom.push(
          new Promise((resolve, reject) => {
            var num = i;
            mysql.data(
              "SELECT * from my_bill a INNER JOIN store b ON a.shop_id=b.s_id WHERE b_id=" +
              b_arr[num],
              [],
              function (err1, rows1, fields) {
                if (err1) {
                  reject(err1);
                  return;
                }
                mysql.data(
                  "SELECT title,gui_ge,new_price,img from my_bill a INNER JOIN bill_goods b INNER JOIN goods c ON a.b_id=b.bill_id AND b.good_id=c.id WHERE b_id=" +
                  b_arr[num],
                  [],
                  function (err1, rows2, fields) {
                    if (err1) {
                      reject(err1);
                      return;
                    }
                    const newObj = { ...rows1[0], good: rows2 };
                    resolve(newObj);
                  }
                );
              }
            );
          })
        );
      }
      Promise.all(prom).then((ress) => {
        res.send(JSON.stringify(ress.reverse()));
      });
    });
});
//积分订单
router.post("/myjfBill", function (req, res, next) {
  mysql.data(
    "SELECT * from jf_bill a INNER JOIN goods b ON a.good_id=b.id",
    [],
    function (err1, rows, fields) {
      if (err1) {
        reject(err1);
        return;
      }
      res.send(JSON.stringify(rows));
    }
  );
}
);
//订单支付
router.post("/pay", function (req, res, next) {
  mysql.data(
    "UPDATE my_bill SET b_type=?,b_site=? where b_id=?",
    [2, '代发货', req.body.b_id],
    function (err1, rows, fields) {
      if (err1) {
        console.log(err1);
        return;
      }
      mysql.data(
        "UPDATE userdata SET wallet=wallet-? where u_id=1",
        [req.body.pay],
        function (err2, rows, fields) {
          if (err1) {
            console.log(err2);
            return;
          }
          mysql.data(
            "select wallet from userdata where u_id=1",
            [],
            function (err2, rows2, fields) {
              if (err2) {
                console.log(err2);
                return;
              }
              mysql.data(
                "INSERT INTO my_record(r_type,r_num,r_time,r_yu) VALUES (?,?,?,?)",
                ['消费', req.body.pay, '2021-12-13', rows2[0].wallet],
                function (err3, rows, fields) {
                  if (err3) {
                    console.log(err3);
                    return;
                  }
                  res.send(JSON.stringify({ code: 1 }));
                }
              );
            }
          );
        }
      );
    }
  );
}
);
//订单退款
router.post("/refund", function (req, res, next) {
  mysql.data(
    "UPDATE my_bill SET b_type=?,b_site=? where b_id=?",
    [5, '已退款', req.body.b_id],
    function (err1, rows, fields) {
      if (err1) {
        console.log(err1);
        return;
      }
      mysql.data(
        "UPDATE userdata SET wallet=wallet+? where u_id=1",
        [req.body.pay],
        function (err2, rows, fields) {
          if (err1) {
            console.log(err2);
            return;
          }
          mysql.data(
            "select wallet from userdata where u_id=1",
            [],
            function (err2, rows2, fields) {
              if (err2) {
                console.log(err2);
                return;
              }
              mysql.data(
                "INSERT INTO my_record(r_type,r_num,r_time,r_yu) VALUES (?,?,?,?)",
                ['退款', req.body.pay, '2021-12-13', rows2[0].wallet],
                function (err3, rows, fields) {
                  if (err3) {
                    console.log(err3);
                    return;
                  }
                  res.send(JSON.stringify({ code: 1 }));
                }
              );
            }
          );
        }
      );
    }
  );
}
);
//我的账单
router.post("/myRec", function (req, res, next) {
  mysql.data(
    "SELECT * from my_record",
    [],
    function (err1, rows, fields) {
      if (err1) {
        console.log(err1);
        return;
      }
      res.send(JSON.stringify(rows.reverse()));
    }
  );
}
);
//删除订单
router.post("/delbill", function (req, res, next) {
  mysql.data(
    "delete from my_bill where b_id=" + req.body.b_id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify({ code: 1 }));
    }
  );
});
module.exports = router;
